package com.example.mardan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mardan.entity.SongciChapter;
import com.example.read.R;

import java.util.List;

public class SongciAdapter extends RecyclerView.Adapter<SongciAdapter.ViewHolder> {
    private List<SongciChapter.ResultDTO> chapters;
    private Context context;

    public SongciAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_songci, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SongciChapter.ResultDTO chapter = chapters.get(position);
        holder.songciName.setText(chapter.getDetailid() + " - " +chapter.getName() + " - " + chapter.getAuthor());
//        holder.itemView.setOnClickListener(v -> {
//            // 启动DetailActivity并传递detailid
//            Intent intent = new Intent(context, SongciDetailActivity.class);
//            intent.putExtra("detailid", chapter.getDetailid());
//            context.startActivity(intent);
//        });
    }

    @Override
    public int getItemCount() {
        return chapters == null ? 0 : chapters.size();
    }

    public void setChapters(List<SongciChapter.ResultDTO> chapters) {
        this.chapters = chapters;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView songciName;

        ViewHolder(View itemView) {
            super(itemView);
            songciName = itemView.findViewById(R.id.songci_name);
        }
    }
}